package com.itany.shop.service.proxy;


import com.itany.shop.entity.Product;
import com.itany.shop.exception.DataAccessException;
import com.itany.shop.exception.ServiceException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.rowmapper.ProductRowMapper;
import com.itany.shop.service.ProductService;
import com.itany.shop.tran.TransactionManager;

import java.util.List;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class ProductServiceProxy implements ProductService {

    private TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productServiceTarget");

    @Override
    public List<Product> findAll() {
        try {
            tran.begin();
            List<Product> products = productService.findAll();
            tran.commit();
            return products;
        } catch (DataAccessException e) {
            tran.rollback();
            throw new ServiceException("查询商品列表失败", e);
        }
    }

@Override
    public List<Product> findPage(String pageNo, String pageSize){
    try {
        tran.begin();
        List<Product> products = productService.findPage(pageNo,pageSize);
        tran.commit();
        return products;
    } catch (DataAccessException e) {
        throw new RuntimeException("查询商品分页失败",e);
    }
}
    @Override
    public Product findById(Integer id) {
        try {
            tran.begin();
            Product products = productService.findById(id);
            tran.commit();
            return products;
        } catch (DataAccessException e) {
            throw new RuntimeException("根据id查询商品失败",e);
        }
    }
}